package com.ciaojian.worker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.base.annotation.ApiIdempotent;
import com.ciaojian.core.enums.business.WorkerApplyStatusEnum;
import com.ciaojian.core.enums.business.WorkerApplyTypeEnum;
import com.ciaojian.core.enums.sys.SysSettingEnum;
import com.ciaojian.core.exception.EntityNotFoundException;
import com.ciaojian.core.model.WorkerApply;
import com.ciaojian.core.model.dto.WorkerApplyDTO;
import com.ciaojian.core.util.Result;
import com.ciaojian.worker.model.param.LeaveApplyParam;
import com.ciaojian.worker.model.param.ReplaceApplyParam;
import com.ciaojian.worker.service.SettingServiceImpl;
import com.ciaojian.worker.service.WorkerApplyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Optional;

/**
 * @author Atlakyn
 */
@Api(tags = "发型师:考勤模块")
@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    @Resource
    private WorkerApplyServiceImpl workerApplyService;
    @Resource
    private SettingServiceImpl settingService;

    @ApiOperation("发型师申请记录")
    @GetMapping("/worker")
    public Result listByWorker(WorkerApplyDTO params,
                               @RequestParam(defaultValue = "0") Integer pageNo,
                               @RequestParam(defaultValue = "6") Integer pageSize) {
        return Result.ok(workerApplyService.listByWorker(params, new Page<>(pageNo, pageSize)));
    }

    @ApiOperation("本月已提交补卡申请的次数")
    @GetMapping("/replace-apply-rate")
    public Result countApplyOfReplace(@NotNull(message = "发型师ID不能为空") Integer workerId) {
        HashMap<String, Integer> map = new HashMap<>(2);
        map.put("count", workerApplyService.countThisMonth(workerId, WorkerApplyTypeEnum.REPLACE));
        map.put("limit", settingService.getBaseMapper().getValueAsInt(SysSettingEnum.REPLACE_RATE.getName()));
        return Result.ok(map);
    }

    @ApiOperation("计算请假时长")
    @GetMapping("/apply-days")
    public Result calculateDays(LeaveApplyParam param) {
        return Result.ok(workerApplyService.calculateDays(param));
    }

    @ApiOperation("补卡申请")
    @ApiIdempotent
    @PostMapping("/replace-apply")
    public Result replace(@RequestBody @Validated ReplaceApplyParam param) {
        // 判断本月补卡次数
        Integer replaceRate = settingService.getBaseMapper().getValueAsInt(SysSettingEnum.REPLACE_RATE.getName());
        Integer count = workerApplyService.countThisMonth(param.getWorkerId(), WorkerApplyTypeEnum.REPLACE);
        Assert.isTrue(count < replaceRate, "您本月补卡次数已达上限");

        return Result.save(workerApplyService.replaceApply(param));
    }

    @ApiOperation("请假申请")
    @ApiIdempotent
    @PostMapping("/leave-apply")
    public Result leaveApply(@RequestBody @Validated LeaveApplyParam param) {
        // 判断是否已有申请还在审核
        WorkerApply workerApply = workerApplyService.getWaitByWorker(param.getWorkerId());
        Assert.isNull(workerApply, "您已有审核在申请,请耐心等待");
        return Result.save(workerApplyService.leaveApply(param));
    }


    @ApiOperation("撤销申请")
    @PutMapping("/apply-withdraw")
    public Result withdraw(Integer workerApplyId) {
        // 判断是否已有申请还在审核
        WorkerApply workerApply = Optional.of(new WorkerApply().selectById(workerApplyId))
                .orElseThrow(() -> new EntityNotFoundException(WorkerApply.class, "id", workerApplyId));
        Assert.isTrue(workerApply.getStatus().equals(WorkerApplyStatusEnum.WAIT), "状态异常");
        workerApplyService.withdraw(workerApply);
        return Result.ok();
    }

}
